// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    /// <summary>
    ///     Denotes that a model class should combine the results of two or more queries into one single result. 
    ///     One or more SqlUnion attributes can be applied to one model class.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = true)]
    public class SqlUnionAttribute : Attribute, ISqlUnion
    {
        /// <summary>
        ///     Initializes a new instance of the SqlUnionAttribute class.
        /// </summary>
        /// <param name="modelType">The type of a model class whose SQL SELECT statement will be combined with the current SQL query.</param>
        public SqlUnionAttribute(Type modelType)
            : this(modelType, null)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the SqlUnionAttribute class.
        /// </summary>
        /// <param name="modelType">The type of a model class whose SQL SELECT statement will be combined with the current SQL query.</param>
        /// <param name="selectName">The name of the select list (SqlSelectAttribute.SelectName property) predefined in modelType.</param>
        public SqlUnionAttribute(Type modelType, string selectName)
        {
            //this.Name = name;
            this.ModelType = modelType;
            this.SelectName = selectName;
            this.IsRawSql = false;

            //Check.NotEmpty(name, nameof(name));
            Check.NotNull(modelType, nameof(modelType));
        }

        /// <summary>
        ///     Initializes a new instance of the SqlUnionAttribute class.
        /// </summary>
        /// <param name="rawSql">The raw SQL SELECT whose result set will be combined with the current SQL query.</param>
        public SqlUnionAttribute(string rawSql)
        {
            //this.Name = name;
            this.RawSql = rawSql;
            this.IsRawSql = true;

            //Check.NotEmpty(name, nameof(name));
            Check.NotEmpty(rawSql, nameof(rawSql));
        }

        //public string Name { get; }

        /// <summary>
        ///     Gets the raw SQL specified for the UNION operator.
        /// </summary>
        public string RawSql { get; }

        /// <summary>
        ///     Gets the type of the model that is specified for the UNION operator.
        /// </summary>
        public Type ModelType { get; }

        /// <summary>
        ///     Gets the name of the select list (SqlSelectAttribute.SelectName property) predefined in 
        ///     the model type specified for the UNION operator.
        /// </summary>
        public string SelectName { get; }

        /// <summary>
        ///     Gets whether a raw SQL has been specified for the UNION operator.
        /// </summary>
        public bool IsRawSql { get; }

        /// <summary>
        ///     Gets or sets whether to use UNION ALL.
        /// </summary>
        public bool UnionAll { get; set; }
    }
}
